<?php

include_once('../includes/utilidades.php');
require_once('../includes/seguridad.php');
include_once('../includes/html.php');

session_start();
$rolesConAcceso = array('club');
if (!rolOK($_SESSION['roles'], $rolesConAcceso))
    exit;

$clubid = $_REQUEST['id'];
$club = select("select * from club", "id=" . $clubid);
$temporada = $_SESSION['temporada'];

$clubRol = "club_" . $clubid;

//print_r($temporada);

print "<h1>" . $club[0]['nombre'] . " (Temporada: " . $temporada[0]['nombre'] . ")</h1>";


$actual = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
$_SESSION['club']['id'] = $club;
$_SESSION['afterAdd']['atleta'] = $actual;
$_SESSION['afterCambiarClub']['atleta'] = $actual;

//$registros = select("select * from vatletatemporadaclub", "idclub=".$clubid);

$indices = select2("select t.id as idtemporada, t.nombre as temporada, c.id as idcategoria,c.nombre as categoria, g.nombre as genero "
        . " from temporada t, categoria c, genero g "
        . " where t.id=c.idtemporada "
        . " and g.id=c.idgenero"
        . " order by c.fechainicio, g.nombre, c.fechafinal");

$sql = "select * from vatletatemporadaclub where idclub=" . $clubid . " and idtemporada=" . $temporada[0]['id'] . " order by categoria,genero,nombre,apellido1,apellido2";
//print $sql;
$atleta = select2($sql);


$atletas = new HTML_Tabla($atleta);
$atletas->setDisplayColumnNames(array('Nombre' => 'Nombre', 'Apellido1' => 'Apellido1', 'Apellido2' => 'Apellido2', 'FechaNacimiento' => 'Año', 'Categoria' => 'Categoria'));
if ((isUserInRol($_SESSION['username'], "admin")) || (isUserInRol($_SESSION['username'], $clubRol))) {
    $atletas->setEnlaces('idatleta', array('Resultados' => '../consultas/verResultadosAtleta.php',
        'Marcas' => '../consultas/cuadroResumenAtleta.php',
        'Cambio Club' => 'cambiarclub.php?idcluborigen=' . $clubid));
} else {
    $atletas->setEnlaces('idatleta', array('Resultados' => 'verResultadosAtleta.php', 'Marcas' => 'cuadroResumenAtleta.php'));
}
$atletas->setBreakPoints(array('Categoria'));
$atletas->toHTML();


$sql = "select c.nombre as categoria,r.nombre as nombre, r.atletas as atletas, r.idcategoria, r.id from relevos r, categoria c where r.idcategoria=c.id and r.idclub=" . $clubid . " and r.idtemporada=" . $temporada[0]['id']. " order by c.nombre";
$relevos = select2($sql);
$tabrev = new HTML_Tabla($relevos);
$tabrev->setDisplayColumnNames(array('categoria'=>'Categoria','nombre'=>'Nombre','atletas'=>'Atletas'));
$tabrev->setBreakPoints(array('categoria'));
$tabrev->setEnlaces('id', array('Editar'=>'../comun/editObjetoForm.php?nombreObjeto=relevos','Borrar'=>'borrarRelevo.php'));
$tabrev->toHTML();



print "<table border=1>";
if ((isUserInRol($_SESSION['username'], "admin")) || (isUserInRol($_SESSION['username'], $clubRol))) {
    $form = "<form name='f' action='altaAtletaClub.php' method='post'>" .
            "<input type=hidden name='idtemporada' value='" . $temporada[0]['id'] . "'/>" .
            "<input type=hidden name='idclub' value='" . $clubid . "'/>" .
            "<input type=submit value='Nuevo Atleta'/>" .
            "</form>";
}
print "<tr><th>Atletas" . $form . "</th>";
if ((isUserInRol($_SESSION['username'], "admin")) || (isUserInRol($_SESSION['username'], $clubRol))) {
    $form = "<form name='f' action='altaRelevo.php' method='post'>" .
            "<input type=hidden name='idtemporada' value='" . $temporada[0]['id'] . "'/>" .
            "<input type=hidden name='idclub' value='" . $clubid . "'/>" .
            "<input type=submit value='Nuevo Relevo'/>" .
            "</form>";
}
print "<th>Relevos" . $form . "</th>";
if ((isUserInRol($_SESSION['username'], "admin")) || (isUserInRol($_SESSION['username'], $clubRol))) {
    $form = "<form name='f' action='altaEquipo.php' method='post'>" .
            "<input type=hidden name='idtemporada' value='" . $temporada[0]['id'] . "'/>" .
            "<input type=hidden name='idclub' value='" . $clubid . "'/>" .
            "<input type=submit value='Nuevo Equipo'/>" .
            "</form>";
}
print "<th>Equipos" . $form . "</th>";
print "</tr>";
print "<tr><td>";
$atletas->printHTML();
print "</td>";
print "<td>";
$tabrev->printHTML();
print "</td>";
print "<td></td>";
print "</tr>";
print "</table>";

//print_r($registros);
$volverA = $_SESSION['afterAdd']['club'];
$actual = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
//print $actual;
$_SESSION['afterAdd']['relevos'] = $actual;
$_SESSION['afterUpdate']['relevos']=$actual;
print "<br><a href=" . $volverA . ">Volver</a>";

//print "<hr>";
//print $actual;
//print "<br>";
// print $_SERVER['PHP_SELF'];
//print_r($_ENV);
//print_r($_SERVER);
?>

